<?php
/*
Template Name: Post Cerita Baru
*/
?>
<?php
if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] )) {

	// Do some minor form validation to make sure there is content
	if (isset($_POST['publish'])) {
    		$error = "";

    	if (!empty($_POST['title'])) {
    		$title = $_POST['title'];
   	 } else {
   	 	$error .= "Judul cerita ";
   	}
	if (!empty($_POST['excerpt'])&& trim($_POST['excerpt'])!=='') {
    		$excerpt = $_POST['excerpt'];
   	 } else {
   		$error .= "Sinopsis ";
   	}
    	if (!empty($_POST['description'])&& trim($_POST['description'])!=='') {
    		$description = $_POST['description'];
   	 } else {
   		$error .= "Isi Cerita ";
   	}
	

	    if (!empty($_POST['post_tags'])) {
    		$post_tags = $_POST['post_tags'];
   	 } else {
   	 	$error .= "Tag ";
   	}

		
		// IMAGE VALIDATION - CHECK IF THERE IS AN IMAGE AND THAT ITS THE RIGHT FILE TYPE AND RIGHT SIZE
		if ($_FILES) {
			foreach ($_FILES as $file => $array) {
				//Check if the $_FILES is set and if the size is > 0 (if =0 it's empty)

				if(isset($_FILES[$file]) && ($_FILES[$file]['size'] > 0)) {

					$tmpName = $_FILES[$file]['tmp_name'];
					list($width, $height, $type, $attr) = getimagesize($tmpName);

				
				// Get the type of the uploaded file. This is returned as "type/extension"
                $arr_file_type = wp_check_filetype(basename($_FILES[$file]['name']));
                $uploaded_file_type = $arr_file_type['type'];

                 // Set an array containing a list of acceptable formats
                $allowed_file_types = array('image/jpg','image/jpeg','image/gif','image/png');

                 // If the uploaded file is the right format
                if(in_array($uploaded_file_type, $allowed_file_types)) {

				} else { // wrong file type
   	 			$error .= "Mohon upload hanya tipe file berikut : JPG, GIF, atau PNG <br />";
                   	 }

				} 
			else {
				$error .= "Gambar ";
				}				
			} // end for each
			
		} // end if

		$tags = $_POST['post_tags'];
		//$winerating = $_POST['winerating'];

		// ADD THE FORM INPUT TO $new_post ARRAY
		if (empty($error)) {
			$new_post = array(
			'post_title'	=>	$title,
			'post_content'	=>	$description,
			'post_excerpt'	=>	$excerpt,
			'post_category'	=>	array($_POST['cat']),  // Usable for custom taxonomies too
			'tags_input'	=>	array($tags),
			'post_status'	=>	'publish',           // Choose: publish, preview, future, draft, etc.
			'post_type'	=>	'post',  //'post',page' or use a custom post type if you want to
			//'winerating'	=>	$winerating
		);

		//SAVE THE POST
		$pid = wp_insert_post($new_post);

        //KEEPS OUR COMMA SEPARATED TAGS AS INDIVIDUAL
		wp_set_post_tags($pid, $_POST['post_tags']);

		//REDIRECT TO THE NEW POST ON SAVE
		$link = get_permalink( $pid );
		wp_redirect( $link );

		//ADD OUR CUSTOM FIELDS 
		add_post_meta($pid, 'rating', $winerating, true); 

			//INSERT OUR MEDIA ATTACHMENTS
			if ($_FILES) {
				foreach ($_FILES as $file => $array) {
				$newupload = insert_attachment($file,$pid);
				// $newupload returns the attachment id of the file that
					// was just uploaded. Do whatever you want with that now.
				}

			} // END THE IF STATEMENT FOR FILES
		} // END SAVING POST
	} // END VALIDATION  END PUBLISH
		else// (isset($_POST['draft'])) 
		{
    		$error = "";

    	if (!empty($_POST['title'])) {
    		$title = $_POST['title'];
   	 } else {
   	 	$error .= "Judul Cerita ";
   	}

    			
		// IMAGE VALIDATION - CHECK IF THERE IS AN IMAGE AND THAT ITS THE RIGHT FILE TYPE AND RIGHT SIZE
		if ($_FILES) {
			foreach ($_FILES as $file => $array) {
				//Check if the $_FILES is set and if the size is > 0 (if =0 it's empty)

				if(isset($_FILES[$file]) && ($_FILES[$file]['size'] > 0)) {

					$tmpName = $_FILES[$file]['tmp_name'];
					list($width, $height, $type, $attr) = getimagesize($tmpName);

			
				// Get the type of the uploaded file. This is returned as "type/extension"
                $arr_file_type = wp_check_filetype(basename($_FILES[$file]['name']));
                $uploaded_file_type = $arr_file_type['type'];

                 // Set an array containing a list of acceptable formats
                $allowed_file_types = array('image/jpg','image/jpeg','image/gif','image/png');

                 // If the uploaded file is the right format
                if(in_array($uploaded_file_type, $allowed_file_types)) {

				} else { // wrong file type
   	 			$error .= "Please upload a JPG, GIF, or PNG file<br />";
                   	 }

				} 
					else {
				$error .= "Gambar ";
				}
			} // end for each
			
		} // end if

		$tags = $_POST['post_tags'];
		//$winerating = $_POST['winerating'];

		// ADD THE FORM INPUT TO $new_post ARRAY
		if (empty($error)) {
			$new_post = array(
			'post_title'	=>	$title,
			'post_content'	=>	$description,
			'post_excerpt'	=>	$excerpt,
			'post_category'	=>	array($_POST['cat']),  // Usable for custom taxonomies too
			'tags_input'	=>	array($tags),
			'post_status'	=>	'draft',           // Choose: publish, preview, future, draft, etc.
			'post_type'	=>	'post',  //'post',page' or use a custom post type if you want to
		
		);

		//SAVE THE POST
		$pid = wp_insert_post($new_post);

        //KEEPS OUR COMMA SEPARATED TAGS AS INDIVIDUAL
		wp_set_post_tags($pid, $_POST['post_tags']);

		//REDIRECT TO THE NEW POST ON SAVE
		$link = get_permalink( $pid );
		wp_redirect( $link );

		//ADD OUR CUSTOM FIELDS 
		add_post_meta($pid, 'rating', $winerating, true); 

			//INSERT OUR MEDIA ATTACHMENTS
			if ($_FILES) {
				foreach ($_FILES as $file => $array) {
				$newupload = insert_attachment($file,$pid);
				// $newupload returns the attachment id of the file that
					// was just uploaded. Do whatever you want with that now.
				}

			} // END THE IF STATEMENT FOR FILES
		} // END SAVING POST
	} // END VALIDATION
} // END THE IF STATEMENT THAT STARTED THE WHOLE FORM

//POST THE POST YO
do_action('wp_insert_post', 'wp_insert_post');

?>

<?php get_header(); ?>

    <div class="col-md-12 column content">
	<div class="row clearfix">
		
		<?php get_template_part( 'kolompertama' ); ?>
	
       
		<div class="col-md-9 column" > <!-- untuk kolom kedua yang dibagi 2 -->
		<div class="row clearfix">
		
				<div class="col-md-12 column">
				 <?php if(is_user_logged_in()){?>
                	<h8 class="judulprofil">Tulis Cerita</h8
					><?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
                    <br/>
                    <br/>
					<?php 
					        if (!empty($error)) {
	                            echo  "<strong>Tidak dapat memproses permintaan Anda, mohon isi dahulu : " . $error . ""  ;
	                        } elseif (!empty($success)) {
	                            echo $success ;
	                        }
	                    ?>
					<form id="new_post" name="new_post" method="post" action="" enctype="multipart/form-data">
                    <div>
                  
                    <div class="list-group-item" style="border-left:none; border-right:none;">
                        <div class="row">
                        <div class="col-md-2">Judul Cerita</div>
                            <div class="col-md-9 ">
                                <div class="col-sm-12">
                                    <input class="form-control tinggitextbox" id="title" type="text" name="title" placeholder="Judul Cerita Wajib diisi" 
									value="<?php if (!empty($_POST['title'])) echo $title ?>">
									
									
                                </div>
                            </div>
                        </div>
                    	</div>
                    <div class="list-group-item" style="border-left:none; border-right:none;">
                        <div class="row">
                        <div class="col-md-2">Kategori</div>
                            <div class="col-md-9 ">
                                <div class="dropdown padd5">
								<?php wp_dropdown_categories( 'show_option_none=Silahkan Pilih Kategori&tab_index=10&taxonomy=category&hide_empty=0&class="btn btn-default dropdown-toggle"&exclude=13,1' ); ?>
                                  </div>
                            </div>
                        </div>
                    </div>
                    <br/>
					 <div class="col-md-2">Sinopsis</div>
		
					<div  class="panel panel-default">
					
						 <textarea class="kotakexcerpt" name="excerpt" id="excerpt" >
						 <?php 
                        if (!empty($_POST['excerpt'])) { echo htmlspecialchars($_POST['excerpt']);} ?>
                        </textarea>
						</div>
						</br>
						 <div class="col-md-2">Isi Cerita</div>
                    <div class="panel panel-default">
						    <div class="panel-heading">
                            <div class="btn-group">
                              <button type="button" class="btn btn-default"><span class="glyphicon glyphicon-align-right"></span></button>
                              <button type="button" class="btn btn-default"><span class="glyphicon glyphicon-align-center"></span></button>
                              <button type="button" class="btn btn-default"><span class="glyphicon glyphicon-align-justify"></span></button>
                              <button type="button" class="btn btn-default"><span class="glyphicon glyphicon-align-left"></span></button>
                              <button type="button" class="btn btn-default"><span class="glyphicon glyphicon-text-height"></span></button>
                              <button type="button" class="btn btn-default"><span class="glyphicon glyphicon-bold"></span></button>
                              <button type="button" class="btn btn-default"><span class="glyphicon glyphicon-italic"></span></button>
                              <div class="btn-group">
                                  <button class="btn btn-default btn-xs dropdown-toggle" type="button" data-toggle="dropdown">
                                    Times New Roman <span class="caret"></span>
                                  </button>
                                      <ul class="dropdown-menu" role="menu">
                                        <li><a href="#" style="background-color:#FFFFFF; border-bottom:#CCCCCC thin dotted; color:#333333">Calibri</a></li>
                                        <li><a href="#" style="background-color:#FFFFFF; border-bottom:#CCCCCC thin dotted; color:#333333">Arial</a></li>
                                        <li><a href="#" style="background-color:#FFFFFF; border-bottom:#CCCCCC thin dotted; color:#333333">San Serif</a></li>
                                      </ul>
                              </div>
                              <div class="btn-group">
                                  <button class="btn btn-default btn-xs dropdown-toggle" type="button" data-toggle="dropdown">
                                    10px <span class="caret"></span>
                                  </button>
                                      <ul class="dropdown-menu" role="menu">
                                        <li><a href="#" style="background-color:#FFFFFF; border-bottom:#CCCCCC thin dotted; color:#333333">12px</a></li>
                                        <li><a href="#" style="background-color:#FFFFFF; border-bottom:#CCCCCC thin dotted; color:#333333">14px</a></li>
                                        <li><a href="#" style="background-color:#FFFFFF; border-bottom:#CCCCCC thin dotted; color:#333333">16px</a></li>
                                      </ul>
                              </div>
                              
                              </div>
                        </div>
                        <textarea class="kotaktext" name="description" id="description" ><?php if (!empty($_POST['description'])) { echo htmlspecialchars($_POST['description']);} ?>
                        </textarea>
                        </div>
                        
                       <div class="row">
                        
                     <!--   <div class="col-xs-6">
                            <div class="row">
                                <div class="col-xs-10 padd6">
                                <div class="input-group input-group-sm">
                                  <h7>   Upload Gambar      </h7>   <input type="file" name="gambar" id="gambar" size="10" accept="image/*" >
                                </div>
                                </div>
                   
                           </div>
                       </div>-->
                       <div class="col-xs-6">
                       <div class="row">
                            <div class="col-xs-10 padd6">
                            <div class="input-group input-group-sm">
                              <h7>Upload Cover</h7>
                           
							  <input type="file" name="cover" id="cover" size="50" accept="image/*" >
							 
                            </div>
                            </div>
                         
                       </div>
                       </div>
                       </div>
                    </div>
                    <br/>
                    <div class="row">
                        <div class="col-xs-6">
                            <div class="input-group input-group-sm padd6">
                                <span class="input-group-addon"><span class="glyphicon glyphicon-tags"></span></span>
                                <input type="text" class="form-control" name="post_tags" id="post_tags" placeholder="pisahkan tag dengan tanda ',' atau ';'" />
                            </div>
                        </div>
                    </div>
                    <br/>
					<input type="submit" value="Simpan Draft" tabindex="40" id="draft" name="draft" class="btn btn-danger btn-sm">
	   				<input type="hidden" name="action" value="draft_post" /></input>
					
					<input type="submit" value="Simpan dan Publish" tabindex="40" id="publish" name="publish" class="btn btn-danger btn-sm">
	   				<input type="hidden" name="action" value="publish_post" /></input>
					<?php wp_nonce_field( 'new-post' ); ?>
                  
                    <button type="submit" class="btn btn-danger btn-sm">Batal</button>
					</form><?php endwhile;} else 
					{?>
					<br/><br/><br/><br/><br/><br/>
					<h9 >Silahkan login terlebih dahulu</h9>
					<?php }					?>
                    </div>
           
			
       </div>      
	   </div>   	
	   
       </div><!-- end row clearfix -->
</div> <!--end col-md-12 column content-->
	<!-- end untuk content -->
   
    
    <!-- untuk footer -->

<?php get_footer(); ?>